package com.xt.core.helper.page;

import com.xt.core.BaseConstants;
import com.xt.core.helper.property.PropertyService;

public class Pager {
    private int totalRows; //total rows
    private int pageSize = BaseConstants.PAGESIZE; //default rows in one page
    
    private int currentPage; //current page number
    private int totalPages; //total page account
    private int startRow; //the first row in this page
    
    public Pager() {
    	try{
			PropertyService propertyService = new PropertyService();
			String tempStr = propertyService.getProperty("pagesize");
			if(tempStr!=null && !"".equals(tempStr)){
				pageSize = new Integer(tempStr).intValue();
			}
		}catch(Exception e){
			pageSize=BaseConstants.PAGESIZE;
		}
    }
    
    public Pager(int _totalRows) {
    	try{
			PropertyService propertyService = new PropertyService();
			String tempStr = propertyService.getProperty("pagesize");
			if(tempStr!=null && !"".equals(tempStr)){
				pageSize = new Integer(tempStr).intValue();
			}
		}catch(Exception e){
			pageSize=BaseConstants.PAGESIZE;
		}
        totalRows = _totalRows;
        totalPages=totalRows/pageSize;
        int mod=totalRows%pageSize;
        if(mod>0){
            totalPages++;
        }
        currentPage = 1;
        startRow = 0;
    }
    
    public Pager(int _totalRows, int _pageSize) {
    	pageSize = _pageSize;
        totalRows = _totalRows;
        totalPages=totalRows/pageSize;
        int mod=totalRows%pageSize;
        if(mod>0){
            totalPages++;
        }
        currentPage = 1;
        startRow = 0;
    }
    
    public int getStartRow() {
        return startRow;
    }
    public int getTotalPages() {
        return totalPages;
    }
    public int getCurrentPage() {
        return currentPage;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }
    public void setStartRow(int startRow) {
        this.startRow = startRow;
    }
    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public int getTotalRows() {
        return totalRows;
    }
    public void first() {
        currentPage = 1;
        startRow = 0;
    }
    public void previous() {
        if (currentPage == 1) {
            return;
        }
        currentPage--;
        startRow = (currentPage - 1) * pageSize;
    }
    public void next() {
        if (currentPage < totalPages) {
            currentPage++;
        }
        startRow = (currentPage - 1) * pageSize;
    }
    public void last() {
        currentPage = totalPages;
        startRow = (currentPage - 1) * pageSize;
    }
    public void refresh(int _currentPage) {
        currentPage = _currentPage;
        if (currentPage > totalPages) {
            last();
        }
    }
    
    /**
     * open the next page directly
     * 
     * @param _nextPage the next page number
     */
    public void open(int _nextPage) {
        currentPage = _nextPage;
        if (currentPage > totalPages) {
            last();
        }else if(currentPage<1){
        	first();
        }else{
        	startRow = (currentPage - 1) * pageSize;
        }
    }
}
